//
//  SAEditShopVC.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/10.
//

#import "SAEditShopVC.h"
#import "SAProductOverModel.h"
#import "UILabel+NSMutableAttributedString.h"
#import "SAEditShopCell.h"
#import "SAProductDetailAPI.h"
#import "SANewGoodsModel.h"
#import "SAProductChooseColorView.h"
#import "SAProductChooseColorModel.h"
#import "SAProductChooseSeasonView.h"
#import "SASelectMerchantsVC.h"
#import "SAProductChooseImageView.h"
#import "SAProductChooseImageModel.h"
#import "SAProductUpdateAPI.h"
#import "SAProductManufacturerModel.h"
#import "ScanHelper.h"
#import "SASingeleCodeVC.h"
#import "SASingeleCodeModel.h"
#import "NSString+FintCommon.h"
#import "RZRichTextView.h"
#import "SAproductUploadAPI.h"

@interface SAEditShopVC ()<UITableViewDataSource, UITableViewDelegate, HTTPAPIDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) UILabel *topTitleLab;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIImageView *headerImageV;
@property (nonatomic, strong) UITextField *productCodeTf;
@property (nonatomic, strong) UITextField *productNameTf;
@property (nonatomic, strong) NSMutableArray *titleMarr;
@property (nonatomic, strong) NSMutableArray *rightMarr;
@property (nonatomic, strong) NSMutableArray *typeMarr;
@property (nonatomic, strong) SAProductDetailAPI *productDetailAPI;
@property (nonatomic, strong) SANewGoodsModel *goodsModel;
@property (strong, nonatomic) NSMutableArray *colorChooseArray;
@property (strong, nonatomic) NSMutableArray *sizeChooseArray;
@property (nonatomic, assign) NSInteger isCompositionDetailId;
@property (strong, nonatomic) NSMutableArray *categoryChooseArray;
@property (nonatomic, strong) NSMutableArray *imageChooseArray;
@property (nonatomic, strong) NSMutableArray *imageServerChooseArray;
@property (nonatomic, assign) NSInteger providerId;
@property (nonatomic, strong) SAProductUpdateAPI *productUpdateAPI;
@property (nonatomic, strong) UIButton *productCodeBtn;

@property (strong, nonatomic) NSMutableArray *colorChooseNameArray;
@property (strong, nonatomic) NSMutableArray *sizeChooseNameArray;

@property (nonatomic, strong) NSMutableArray *seasonChooseArray;
@property (nonatomic, strong) NSMutableArray *brandChooseArray;
@property (nonatomic, strong) NSMutableArray *genderChooseArray;
@property (nonatomic, strong) NSMutableArray *unitChooseArray;

@property (nonatomic, strong) NSMutableArray *contentChooseArray;
@property (nonatomic, strong) NSMutableArray *performChooseArray;
@property (nonatomic, strong) NSMutableArray *securityChooseArray;
@property (nonatomic, strong) NSMutableArray *qualityChooseArray;
@property (nonatomic, strong) NSMutableArray *washingChooseArray;

@property (nonatomic, assign) NSInteger seasonDetailId;
@property (nonatomic, assign) NSInteger brandDetailId;
@property (nonatomic, assign) NSInteger genderDetailId;
@property (nonatomic, assign) NSInteger unitDetailId;

@property (nonatomic, assign) NSInteger contentDetailId;
@property (nonatomic, assign) NSInteger performDetailId;
@property (nonatomic, assign) NSInteger securityDetailId;
@property (nonatomic, assign) NSInteger qualityDetailId;
@property (nonatomic, assign) NSInteger washingDetailId;

@property (nonatomic, strong) RZRichTextView *footview;
@property (nonatomic, strong) SAproductUploadAPI *productUploadAPI;
@property (nonatomic, strong) NSMutableArray *imageMarr;

@end

@implementation SAEditShopVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.productDetailAPI == api) {
        self.goodsModel = [SANewGoodsModel mj_objectWithKeyValues:data[@"data"]];
        [self.colorChooseArray removeAllObjects];
        [self.sizeChooseArray removeAllObjects];
        [self.colorChooseNameArray removeAllObjects];
        [self.sizeChooseNameArray removeAllObjects];
        NSMutableArray *colorMarr = [NSMutableArray array];
        for (SAProductColoursList *model in self.goodsModel.colours) {
            if (model.attrName && model.attrName.length > 0) {
                [colorMarr addObject:model.attrName];
                [self.colorChooseNameArray addObject:model.attrName];
                [self.colorChooseArray addObject:[NSString stringWithFormat:@"%zd",model.specsAttrId]];
            }
        }
        NSMutableArray *sizeMarr = [NSMutableArray array];
        for (SAProductSizesList *model in self.goodsModel.sizes) {
            if (model.attrName && model.attrName.length > 0) {
                [sizeMarr addObject:model.attrName];
                [self.sizeChooseNameArray addObject:model.attrName];
                [self.sizeChooseArray addObject:[NSString stringWithFormat:@"%zd",model.specsAttrId]];
            }
        }
        
        for (SAProductToAttrFormsList *model in self.goodsModel.attrForm) {
            if (model.attrType == 1) {
                self.seasonDetailId = model.attrId;
                self.rightMarr[3][2] = model.attrName;
            }else if (model.attrType == 2) {
                self.brandDetailId = model.attrId;
                self.rightMarr[3][3] = model.attrName;
            }else if (model.attrType == 3) {
                self.genderDetailId = model.attrId;
                self.rightMarr[3][4] = model.attrName;
            }else if (model.attrType == 4) {
                self.isCompositionDetailId = model.attrId;
                self.rightMarr[3][6] = model.attrName;
            }else if (model.attrType == 5) {
                self.contentDetailId = model.attrId;
                self.rightMarr[3][8] = model.attrName;
            }else if (model.attrType == 6) {
                self.performDetailId = model.attrId;
                self.rightMarr[3][9] = model.attrName;
            }else if (model.attrType == 7) {
                self.securityDetailId = model.attrId;
                self.rightMarr[3][10] = model.attrName;
            }else if (model.attrType == 8) {
                self.qualityDetailId = model.attrId;
                self.rightMarr[3][11] = model.attrName;
            }else if (model.attrType == 9) {
                self.washingDetailId = model.attrId;
                self.rightMarr[3][12] = model.attrName;
            }
        }
        if (self.goodsModel.unitName.length > 0) {
            self.unitDetailId = self.goodsModel.unitId;
            self.rightMarr[3][5] = self.goodsModel.unitName;
        }
        
        [self.imageChooseArray removeAllObjects];
        [self.imageServerChooseArray removeAllObjects];
        if (self.goodsModel.imgUrl.count > 0) {
            self.headerImageV.contentMode = UIViewContentModeScaleAspectFit;
            NSString *url = self.goodsModel.imgUrl[self.goodsModel.imgIndex];
            [self.headerImageV sd_setImageWithURL:[NSURL URLWithString:url]];
            for (int i = 0; i < self.goodsModel.imgUrl.count; i++) {
                SAProductChooseImageModel *model = [SAProductChooseImageModel new];
                model.url = self.goodsModel.imgUrl[i];
                if (i == self.goodsModel.imgIndex) {
                    model.isSelecd = YES;
                }else {
                    model.isSelecd = NO;
                }
                [self.imageChooseArray addObject:model];
                [self.imageServerChooseArray addObject:self.goodsModel.imgUrl[i]];
            }
        }
        
        self.rightMarr[0][0] = [colorMarr componentsJoinedByString:@","];
        self.rightMarr[0][1] = [sizeMarr componentsJoinedByString:@","];
        self.rightMarr[1][0] = [NSString stringWithFormat:@"%0.2f",[self.goodsModel.entryPrice doubleValue] /100.f];
        self.rightMarr[1][1] = [NSString stringWithFormat:@"%0.2f",[self.goodsModel.retailPrice doubleValue] /100.f];
        self.rightMarr[1][2] = [NSString stringWithFormat:@"%0.2f",[self.goodsModel.microStorePrice doubleValue] /100.f];
        self.rightMarr[1][3] = [NSString stringWithFormat:@"%0.f",self.goodsModel.discountRate /100.f];
        self.rightMarr[2][0] = self.goodsModel.barcode;
        NSString *setStr = @"";
        for (SASkuFormModel *model in self.goodsModel.skuForm) {
            if (model.barcode.length > 0) {
                setStr = @"已设置";
            }
        }
        self.rightMarr[2][1] = setStr;
        if (self.goodsModel.providerName.length > 0) {
            self.rightMarr[3][7] = self.goodsModel.providerName;
        }
        
        if (self.goodsModel.ground.length > 0) {
            self.rightMarr[3][0] = self.goodsModel.ground;
        }
        if (self.goodsModel.dealer.length > 0) {
            self.rightMarr[3][1] = self.goodsModel.dealer;
        }
        
        if ([SAValueManger shareInStance].userInfo.storeName.length > 0) {
            self.rightMarr[3][13] = self.goodsModel.storeName;
        }
        if (self.goodsModel.onsales == 1) {
            self.rightMarr[3][14] = @"1";
        }else {
            self.rightMarr[3][14] = @"0";
        }
        
        if (self.goodsModel.isMicroStoreProduct == 1) {
            self.rightMarr[3][15] = @"1";
            //            self.tableView.tableFooterView = self.footview;
            self.tableView.tableFooterView = nil;
        }else {
            self.rightMarr[3][15] = @"0";
            self.tableView.tableFooterView = nil;
        }
        
        self.footview.attributedText = [NSAttributedString htmlString:self.goodsModel.microStoreIntro];
        //        self.footview.height = self.footview.contentSize.height + 50;
        
        self.providerId = self.goodsModel.providerId;
        [self.tableView reloadData];
    }else if (self.productUpdateAPI == api) {
        [HDHubView dismissLoading];
        [Prompt popPromptViewWithMsg:@"编辑成功" duration:2];
        [self.navigationController popViewControllerAnimated:YES];
        !self.refreshBlock ?: self.refreshBlock();
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    [HDHubView dismissLoading];
}


#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.titleMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *arr = self.titleMarr[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak typeof(self) weakSelf = self;
    SAEditShopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAEditShopCell" forIndexPath:indexPath];
    NSString *leftTitle = self.titleMarr[indexPath.section][indexPath.row];
    NSString *rightTitle = self.rightMarr[indexPath.section][indexPath.row];
    NSString *type = self.typeMarr[indexPath.section][indexPath.row];
    cell.date = self.goodsModel.onsalesTime;
    [cell refreshLeftTitle:leftTitle RightTitle:rightTitle Type:type.integerValue];
    cell.onBlock = ^(NSString * _Nonnull index) {
        if (indexPath.row == 14) {
            weakSelf.rightMarr[3][14] = index;
            if ([index isEqualToString:@"1"]) {
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                weakSelf.goodsModel.onsalesTime = [dateFormatter stringFromDate:[NSDate date]];
            }
        }else {
            weakSelf.rightMarr[3][indexPath.row] = index;
            if ([index isEqualToString:@"1"]) {
                //                tableView.tableFooterView = weakSelf.footview;
                tableView.tableFooterView = nil;
            }else {
                tableView.tableFooterView = nil;
            }
        }
        [tableView reloadData];
    };
    cell.inputBlock = ^(NSString * _Nonnull str) {
        weakSelf.rightMarr[indexPath.section][indexPath.row] = str;
    };
    cell.singleCodeBlock = ^{
        NSMutableArray<SASingeleCodeModel *> *singleMarr = [NSMutableArray array];
        NSMutableArray *colorChooseMarr = [NSMutableArray array];
        NSMutableArray *colorChooseNameMarr = [NSMutableArray array];
        for (NSString *str in weakSelf.colorChooseArray) {
            [colorChooseMarr addObject:str];
        }
        for (NSString *str in weakSelf.colorChooseNameArray) {
            [colorChooseNameMarr addObject:str];
        }
        if (colorChooseMarr.count == 0) {
            [colorChooseMarr addObject:@"1"];
            [colorChooseNameMarr addObject:@"均色"];
        }
        
        NSMutableArray *sizeChooseMarr = [NSMutableArray array];
        NSMutableArray *sizeChooseNameMarr = [NSMutableArray array];
        for (NSString *str in weakSelf.sizeChooseArray) {
            [sizeChooseMarr addObject:str];
        }
        for (NSString *str in weakSelf.sizeChooseNameArray) {
            [sizeChooseNameMarr addObject:str];
        }
        if (sizeChooseMarr.count == 0) {
            [sizeChooseMarr addObject:@"2"];
            [sizeChooseNameMarr addObject:@"均码"];
        }
        
        for (int i = 0; i < colorChooseMarr.count; i++) {
            SASingeleCodeModel *model = [SASingeleCodeModel new];
            model.colourSpecsAttrId = colorChooseMarr[i];
            model.colourAttrName = colorChooseNameMarr[i];
            model.skuMarr = [NSMutableArray array];
            [singleMarr addObject:model];
            for (int j = 0; j < sizeChooseMarr.count; j++) {
                SASkuFormModel *skuModel = [SASkuFormModel new];
                skuModel.colourId = colorChooseMarr[i];
                skuModel.colourName = colorChooseNameMarr[i];
                skuModel.sizeId = sizeChooseMarr[j];
                skuModel.sizeName = sizeChooseNameMarr[j];
                skuModel.barcode = @"";
                [model.skuMarr addObject:skuModel];
                for (SASkuFormModel *skuFormModel in weakSelf.goodsModel.skuForm) {
                    if ([skuModel.colourId isEqualToString:skuFormModel.colourId] && [skuModel.sizeId isEqualToString:skuFormModel.sizeId]) {
                        skuModel.barcode = skuFormModel.barcode;
                    }
                }
            }
        }
        SASingeleCodeVC *pushVC = [[SASingeleCodeVC alloc] initWithSingeleCodeModelMarr:singleMarr];
        [weakSelf.navigationController pushViewController:pushVC animated:YES];
        pushVC.confirmBlock = ^(NSMutableArray<SASingeleCodeModel *> * _Nonnull marr) {
            [weakSelf singleCode:marr];
        };
    };
    cell.productCodeBlock = ^{
        [weakSelf scanProductCode];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0){
        [self colorChooseBtnClick];
    }else if (indexPath.section == 0 && indexPath.row == 1){
        [self sizeChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 2){
        [self seasonChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 3){
        [self brandChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 4){
        [self genderChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 5){
        [self unitChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 6){
        [self typeChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 7){
        [self SelectMerchantsBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 8){
        [self compositionChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 9){
        [self performChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 10){
        [self securityChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 11){
        [self qualityChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 12){
        [self washingChooseBtnClick];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(40);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == self.titleMarr.count -1) {
        NSMutableArray *marr = self.rightMarr[section];
        NSString *rightTitle = marr.lastObject;
        if ([rightTitle isEqualToString:@"1"]) {
            return 0.001;
        }
        return SafeFooterHeight;
    }
    return kScaleNum(10);
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (self.productCodeTf == textField) {
        if (self.goodsModel.nameCodeEdit != YES) {
            [Prompt popPromptViewWithMsg:@"已有库存流水的商品，无法修改款号!" duration:2];
            return NO;
        }
    }
    return YES;
}

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 14) {
        return NO;
    }
    
    return [self validateNumber:string];
}

- (void)textFieldEditChanged:(UITextField*)textField {
    NSString *newStr = [textField.text noEmoji];
    if (textField.text.length != newStr.length) {
        textField.text = newStr;
    }
}

#pragma mark - event
//颜色选择
-(void)colorChooseBtnClick {
    [self.view endEditing:YES];
    if (self.goodsModel.coloursChoice == NO) {
        [Prompt popPromptViewWithMsg:@"当前颜色已有库存！暂不能修改" duration:2];
        return;
    }
    SAProductChooseColorView *chooseColorView = [SAProductChooseColorView shareCornView];
    [chooseColorView loadData:self.colorChooseArray andType:@"1" andIsDetail:YES andArray:@[] andType:self.productOverModel.productId];
    [chooseColorView showView:self];
    
    __weak typeof(self) weakSelf = self;
    chooseColorView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        NSMutableArray *colorMarr = [NSMutableArray array];
        [weakSelf.colorChooseArray removeAllObjects];
        [weakSelf.colorChooseNameArray removeAllObjects];
        for (SAProductChooseColorModel *productModel in selectedArray) {
            for (SAProductChooseColorList *model in productModel.attrVOS) {
                if (model.check == YES) {
                    [colorMarr addObject:model.attrName];
                    [weakSelf.colorChooseNameArray addObject:model.attrName];
                    [weakSelf.colorChooseArray addObject:model.specsAttrId];
                }
            }
        }
        weakSelf.rightMarr[0][0] = [colorMarr componentsJoinedByString:@","];
        [weakSelf.tableView reloadData];
    };
}

//尺码选择
-(void)sizeChooseBtnClick {
    [self.view endEditing:YES];
    if (self.goodsModel.sizesChoice == NO) {
        [Prompt popPromptViewWithMsg:@"当前尺码已有库存！暂不能修改" duration:2];
        return;
    }
    SAProductChooseColorView *chooseColorView = [SAProductChooseColorView shareCornView];
    [chooseColorView loadData:self.sizeChooseArray andType:@"2" andIsDetail:YES andArray:@[] andType:self.productOverModel.productId];
    [chooseColorView showView:self];
    
    __weak typeof(self) weakSelf = self;
    chooseColorView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        NSMutableArray *sizeMarr = [NSMutableArray array];
        [weakSelf.sizeChooseArray removeAllObjects];
        [weakSelf.sizeChooseNameArray removeAllObjects];
        for (SAProductChooseColorModel *productModel in selectedArray) {
            for (SAProductChooseColorList *model in productModel.attrVOS) {
                if (model.check == YES) {
                    [sizeMarr addObject:model.attrName];
                    [weakSelf.sizeChooseNameArray addObject:model.attrName];
                    [weakSelf.sizeChooseArray addObject:model.specsAttrId];
                }
            }
        }
        weakSelf.rightMarr[0][1] = [sizeMarr componentsJoinedByString:@","];
        [weakSelf.tableView reloadData];
    };
}

//类别选择
-(void)typeChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.categoryChooseArray andType:@"4" andIsDetail:@"1" andIsDetailId:self.isCompositionDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.categoryChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][6] = list.attrName;
                weakSelf.isCompositionDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//选择图片
-(void)ChooseImage {
    [self.view endEditing:YES];
    SAProductChooseImageView *chooseImageView = [SAProductChooseImageView shareCornView];
    [chooseImageView loadData:self.imageChooseArray andServeArray:self.imageServerChooseArray andType:@"选择图片"];
    [chooseImageView showView:self];
    __weak typeof(self) weakSelf = self;
    chooseImageView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray, NSMutableArray * _Nonnull serverArray) {
        weakSelf.imageChooseArray = selectedArray;
        weakSelf.imageServerChooseArray = serverArray;
        for (SAProductChooseImageModel *model in selectedArray) {
            if (model.isSelecd == YES) {
                [weakSelf.headerImageV sd_setImageWithURL:[NSURL URLWithString:model.url]];
                weakSelf.headerImageV.contentMode = UIViewContentModeScaleAspectFit;
            }
        }
        if (self.imageChooseArray.count == 0) {
            weakSelf.headerImageV.image = [UIImage imageNamed:@"Shop_Camera"];
            weakSelf.headerImageV.contentMode = UIViewContentModeCenter;
        }
    };
}

//选择厂家
-(void)SelectMerchantsBtnClick {
    __weak typeof(self) weakSelf = self;
    [self.view endEditing:YES];
    
    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
    NSString *isSupplier = [NSString stringWithFormat:@"%@",menuList[7][@"isDelete"]];
    NSString *seeProvider = [NSString stringWithFormat:@"%@",menuList[7][@"perms"][@"seeProvider"]];
    if ([isSupplier isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有供货商权限,请联系店长添加" duration:2];
        return;
    }
    if ([seeProvider isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有供货商权限,请联系店长添加" duration:2];
        return;
    }
    
    SASelectMerchantsVC *pushVC = [SASelectMerchantsVC new];
    pushVC.selectBlock = ^(SAProductManufacturerModel *model) {
        weakSelf.providerId = model.providerId;
        weakSelf.rightMarr[3][7] = model.providerName;
        [weakSelf.tableView reloadData];
    };
    [self.navigationController pushViewController:pushVC animated:YES];
}

//季节选择
-(void)seasonChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.seasonChooseArray andType:@"1" andIsDetail:@"1" andIsDetailId:self.seasonDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.seasonChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][2] = list.attrName;
                weakSelf.seasonDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//品牌选择
-(void)brandChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.brandChooseArray andType:@"2" andIsDetail:@"1" andIsDetailId:self.brandDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.brandChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][3] = list.attrName;
                weakSelf.brandDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//性别选择
-(void)genderChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.genderChooseArray andType:@"3" andIsDetail:@"1" andIsDetailId:self.genderDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.genderChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][4] = list.attrName;
                weakSelf.genderDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//单位选择
-(void)unitChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.unitChooseArray andType:@"-1" andIsDetail:@"1" andIsDetailId:self.unitDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.unitChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][5] = list.unitName;
                weakSelf.unitDetailId = list.unitId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//成分含量
-(void)compositionChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.contentChooseArray andType:@"5" andIsDetail:@"1" andIsDetailId:self.contentDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.contentChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][8] = list.attrName;
                weakSelf.contentDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//执行标准
-(void)performChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.performChooseArray andType:@"6" andIsDetail:@"1" andIsDetailId:self.performDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.performChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][9] = list.attrName;
                weakSelf.performDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//安全类别
-(void)securityChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.securityChooseArray andType:@"7" andIsDetail:@"1" andIsDetailId:self.securityDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.securityChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][10] = list.attrName;
                weakSelf.securityDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//质量等级
-(void)qualityChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.qualityChooseArray andType:@"8" andIsDetail:@"1" andIsDetailId:self.qualityDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.qualityChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][11] = list.attrName;
                weakSelf.qualityDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//洗涤方式
-(void)washingChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.washingChooseArray andType:@"9" andIsDetail:@"1" andIsDetailId:self.washingDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.washingChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][12] = list.attrName;
                weakSelf.washingDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

-(void)clickedProductCodeBtn {
    if (self.goodsModel.nameCodeEdit != YES) {
        [Prompt popPromptViewWithMsg:@"已有库存流水的商品，无法修改款号!" duration:2];
        return;
    }
    [self.navigationController pushViewController:[[ScanHelper shareInstance] ScanVCWithStyle:notSquare qrResultCallBack:^(id result) {
        self.productCodeTf.text = result;
        [self.navigationController popViewControllerAnimated:NO];
    }] animated:YES];
}

//扫码
-(void)scanProductCode {
    [self.navigationController pushViewController:[[ScanHelper shareInstance] ScanVCWithStyle:notSquare qrResultCallBack:^(id result) {
        self.rightMarr[2][0] = result;
        [self.tableView reloadData];
        [self.navigationController popViewControllerAnimated:NO];
    }] animated:YES];
}

//单品条码
-(void)singleCode:(NSMutableArray *)marr {
    NSString *str = @"";
    NSMutableArray<SASkuFormModel *> *skuFormMarr = [NSMutableArray array];
    for (SASingeleCodeModel *model in marr) {
        for (SASkuFormModel *skuModel in model.skuMarr) {
            if (skuModel.barcode.length > 0) {
                str = @"已设置";
            }
            [skuFormMarr addObject:skuModel];
        }
    }
    self.rightMarr[2][1] = str;
    self.goodsModel.skuForm = skuFormMarr;
    [self.tableView reloadData];
}

-(void)clickedConfirmBtn {
    
    [self.view endEditing:YES];
    if (trimString(self.productNameTf.text).length <= 0) {
        [Prompt popPromptViewWithMsg:@"请输入名称" duration:2];
        return;
    }
    
    NSArray *imageArr = self.footview.rz_richTextImages;
    self.imageMarr = [NSMutableArray array];
    
    [HDHubView loadingMessage:@"正在保存"];
    if (imageArr.count > 0) {
        [self.productUploadAPI postDataWithUrl:@"" params:nil imageDatas:imageArr success:^(id response) {
            NSLog(@"%@",response);
            if ([response[@"code"] integerValue] == 0) {
                self.imageMarr = response[@"data"];
                [self clickedConfirmBtn2];
            }
        } failure:^(id error) {
            [Prompt popPromptViewWithMsg:@"保存失败" duration:2];
            [HDHubView dismissLoading];
        }];
    }else {
        [self clickedConfirmBtn2];
    }
    
}

-(void)clickedConfirmBtn2 {
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    dict[@"productId"] = @(self.productOverModel.productId);
    dict[@"productName"] = trimString(self.productNameTf.text);
    if (trimString(self.productCodeTf.text).length == 0) {
        dict[@"productCode"] = self.goodsModel.productCode;
    }else {
        dict[@"productCode"] = trimString(self.productCodeTf.text);
    }
    dict[@"onsalesTime"] = self.goodsModel.onsalesTime;
    dict[@"remark"] = self.goodsModel.remark;
    dict[@"stockDown"] = @(self.goodsModel.stockDown);
    dict[@"stockUp"] = @(self.goodsModel.stockUp);
    dict[@"unitId"] = @(self.goodsModel.unitId);
    dict[@"unitName"] = self.goodsModel.unitName;
    if (self.unitChooseArray.count >0) {
        for ( SAProductChooseColorList *list in self.unitChooseArray) {
            if (list.check == YES) {
                dict[@"unitId"] =@(list.unitId);
                dict[@"unitName"] =list.unitName;
            }
        }
    }
    NSString *barcode = self.rightMarr[2][0];
    if (barcode.length == 0) {
        barcode = [NSString stringWithFormat:@"%@%@",[self generateTradeNO],dict[@"productCode"]];
    }
    dict[@"barcode"] = barcode;
    
    if ([self.rightMarr[3][14] isEqualToString:@"1"]) {
        dict[@"upper"] = @(true);
    }else {
        dict[@"upper"] = @(false);
    }
    
    if ([self.rightMarr[3][15] isEqualToString:@"1"]) {
        dict[@"isMicroStoreProduct"] = @"1";
    }else {
        dict[@"isMicroStoreProduct"] = @"0";
    }
    
    //    dict[@"microStoreIntro"] = [self.footview rz_codingToHtmlWithImageURLS:self.imageMarr];
    
    if (self.colorChooseArray.count > 0) {
        dict[@"colours"] = self.colorChooseArray;
    }
    
    if (self.sizeChooseArray.count > 0) {
        dict[@"sizes"] = self.sizeChooseArray;
    }
    
    long max = 1000;
    for (SASkuFormModel *skuFormModel in self.goodsModel.skuForm) {
        NSString *barcode = skuFormModel.barcode;
        if ([barcode containsString:dict[@"productCode"]]) {
            barcode = [barcode stringByReplacingOccurrencesOfString:dict[@"productCode"] withString:@""];
            long barcodeD = [barcode longLongValue];
            if (barcodeD > max) {
                max = barcodeD;
            }
        }
    }
    
    
    NSMutableArray *newColorChooseArray = self.colorChooseArray;
    NSMutableArray *newSizeChooseArray = self.sizeChooseArray;
    if (newColorChooseArray.count == 0) {
        [newColorChooseArray addObject:@"1"];
    }
    if (newSizeChooseArray.count == 0) {
        [newSizeChooseArray addObject:@"2"];
    }
    
    NSMutableArray *colorAndSizeArray = [NSMutableArray array];
    NSInteger i = 0;
    NSInteger j = 0;
    for (NSString *colorId in newColorChooseArray) {
        for  (NSString *sizeId in newSizeChooseArray) {
            NSMutableDictionary *dicColorAndSize = [[NSMutableDictionary alloc] init];
            dicColorAndSize[@"colourId"]  = colorId;
            dicColorAndSize[@"sizeId"]  = sizeId;
            dicColorAndSize[@"stock"]  = @"0";
            [colorAndSizeArray addObject:dicColorAndSize];
            NSString *barcode = @"";
            for (SASkuFormModel *skuFormModel in self.goodsModel.skuForm) {
                if ([colorId isEqualToString:skuFormModel.colourId] && [sizeId isEqualToString:skuFormModel.sizeId]) {
                    barcode = skuFormModel.barcode;
                }
            }
            if (barcode.length > 0) {
                dicColorAndSize[@"barcode"] = barcode;
            }else {
                max =  max + 1;
                dicColorAndSize[@"barcode"] = [NSString stringWithFormat:@"%ld%@",max,dict[@"productCode"]];
            }
            j++;
        }
        i++;
        j = 0;
    }
    dict[@"skuForm"] = colorAndSizeArray;
    
    if ([self.rightMarr[1][0] length] >0) {
        dict[@"entryPrice"] = [NSString stringWithFormat:@"%0.lf",[self.rightMarr[1][0] doubleValue]*100];
    }
    if ([self.rightMarr[1][1] length] >0) {
        NSString *retailPrice = self.rightMarr[1][1];
        double retailPriceD = [retailPrice doubleValue] * 100;
        dict[@"retailPrice"] = [NSString stringWithFormat:@"%0.lf",retailPriceD];
    }
    if ([self.rightMarr[1][2] length] >0) {
        dict[@"microStorePrice"] = [NSString stringWithFormat:@"%0.lf",[self.rightMarr[1][2] doubleValue]*100];
    }
    if ([self.rightMarr[1][3] length] >0) {
        dict[@"discountRate"] = [NSString stringWithFormat:@"%0.lf",[self.rightMarr[1][3] doubleValue]];
    }
    
    if (self.imageServerChooseArray.count >0) {
        dict[@"imgUrl"] = self.imageServerChooseArray;
        for (int i=0; i<self.imageChooseArray.count; i++) {
            SAProductChooseImageModel *model = self.imageChooseArray[i];
            if (model.isSelecd == YES) {
                dict[@"imgIndex"] = @(i);
            }
        }
    }
    
    NSMutableArray *chooseArray = [[NSMutableArray alloc] init];
    if ([self.rightMarr[3][2] length] > 0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        chooseDict[@"attrId"] =@(self.seasonDetailId);
        chooseDict[@"attrName"] = self.rightMarr[3][2];
        chooseDict[@"attrType"] =@(1);
        chooseDict[@"check"] = @"true";
        [chooseArray addObject:chooseDict];
    }
    if ([self.rightMarr[3][3] length] > 0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        chooseDict[@"attrId"] =@(self.brandDetailId);
        chooseDict[@"attrName"] = self.rightMarr[3][3];
        chooseDict[@"attrType"] =@(2);
        chooseDict[@"check"] = @"true";
        [chooseArray addObject:chooseDict];
    }
    if ([self.rightMarr[3][4] length] > 0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        chooseDict[@"attrId"] =@(self.genderDetailId);
        chooseDict[@"attrName"] = self.rightMarr[3][4];
        chooseDict[@"attrType"] =@(3);
        chooseDict[@"check"] = @"true";
        [chooseArray addObject:chooseDict];
    }
    
    if ([self.rightMarr[3][6] length] > 0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        chooseDict[@"attrId"] =@(self.isCompositionDetailId);
        chooseDict[@"attrName"] = self.rightMarr[3][6];
        chooseDict[@"attrType"] =@(4);
        chooseDict[@"check"] = @"true";
        [chooseArray addObject:chooseDict];
    }
    if ([self.rightMarr[3][8] length] > 0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        chooseDict[@"attrId"] =@(self.contentDetailId);
        chooseDict[@"attrName"] = self.rightMarr[3][8];
        chooseDict[@"attrType"] =@(5);
        chooseDict[@"check"] = @"true";
        [chooseArray addObject:chooseDict];
    }
    if ([self.rightMarr[3][9] length] > 0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        chooseDict[@"attrId"] =@(self.performDetailId);
        chooseDict[@"attrName"] = self.rightMarr[3][9];
        chooseDict[@"attrType"] =@(6);
        chooseDict[@"check"] = @"true";
        [chooseArray addObject:chooseDict];
    }
    if ([self.rightMarr[3][10] length] > 0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        chooseDict[@"attrId"] =@(self.securityDetailId);
        chooseDict[@"attrName"] = self.rightMarr[3][10];
        chooseDict[@"attrType"] =@(7);
        chooseDict[@"check"] = @"true";
        [chooseArray addObject:chooseDict];
    }
    if ([self.rightMarr[3][11] length] > 0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        chooseDict[@"attrId"] =@(self.qualityDetailId);
        chooseDict[@"attrName"] = self.rightMarr[3][11];
        chooseDict[@"attrType"] =@(8);
        chooseDict[@"check"] = @"true";
        [chooseArray addObject:chooseDict];
    }
    if ([self.rightMarr[3][12] length] > 0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        chooseDict[@"attrId"] =@(self.washingDetailId);
        chooseDict[@"attrName"] = self.rightMarr[3][12];
        chooseDict[@"attrType"] =@(9);
        chooseDict[@"check"] = @"true";
        [chooseArray addObject:chooseDict];
    }
    
    if (chooseArray.count > 0) {
        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:chooseArray options:NSJSONWritingPrettyPrinted error:&error];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        dict[@"toAttrForms"] = jsonString;
    }
    
    if (trimString(self.rightMarr[3][0]).length >0) {
        dict[@"ground"] = trimString(self.rightMarr[3][0]);
    }
    
    if (trimString(self.rightMarr[3][1]).length >0) {
        dict[@"dealer"] = trimString(self.rightMarr[3][1]);
    }
    
    if (trimString(self.rightMarr[3][7]).length >0) {
        dict[@"providerId"] = @(self.providerId);
    }
    
    self.productUpdateAPI.coverParameters = dict;
    [self.productUpdateAPI sendRequestToServerWithActivityViewAndMask:POST];
    
}

- (NSString *)generateTradeNO
{
    static int kNumber = 3;
    NSString *sourceStr = @"0123456789";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand((unsigned)time(0));
    for (int i = 0; i < kNumber; i++)
    {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    return resultStr;
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678\\9*-"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBI:@"" AndTitle:@""];
    [self addRightViews:@[self.topView]];
    [self.topView addSubview:self.topTitleLab];
    [self.topView addSubview:self.confirmBtn];
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    [self.headerView addSubview:self.headerImageV];
    [self.headerView addSubview:self.productCodeTf];
    [self.headerView addSubview:self.productNameTf];
    //    [self.headerView addSubview:self.productCodeBtn];
    
    self.topTitleLab.text = [NSString stringWithFormat:@"%@\n%@",self.productOverModel.productCode,self.productOverModel.productName];
    [self.topTitleLab setAttributedStringWithSubString:self.productOverModel.productName color:kGrayColor font:kFont(13)];
    self.productCodeTf.text = self.productOverModel.productCode;
    self.productNameTf.text = self.productOverModel.productName;
    
    self.productDetailAPI.productId = [NSString stringWithFormat:@"%zd",self.productOverModel.productId];
    [self.productDetailAPI sendRequestToServerWithActivityViewAndMask:GET];
    
    RZRichTextView *footview = [[RZRichTextView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth, 245)];
    footview.font = [UIFont systemFontOfSize:16.0];
    self.footview = footview;
    //    self.tableView.tableFooterView = footview;
    self.tableView.tableFooterView = nil;
    self.footview.bounces = NO;
    self.tableView.bounces = NO;
    //    footview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    
    UILabel *placeholderLb = [[UILabel alloc] init];
    placeholderLb.font = self.footview.font;
    placeholderLb.textColor = kGrayColor;
    placeholderLb.text = @"请输入文字或插入图片";
    placeholderLb.numberOfLines = 0;
    [footview addSubview:placeholderLb];
    [footview setValue:placeholderLb forKey:@"_placeholderLabel"];
    footview.text = @"1";
    footview.text = @"";
}

#pragma mark - lazy
-(UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:SARectMake(63, 0, 312, 44)];
    }
    return _topView;
}

-(UILabel *)topTitleLab {
    if (!_topTitleLab) {
        _topTitleLab = [[UILabel alloc] initWithFrame:SARectMake(0, 0, 249, 44)];
        _topTitleLab.font = kFont(16);
        _topTitleLab.numberOfLines = 0;
        _topTitleLab.textAlignment = NSTextAlignmentCenter;
        _topTitleLab.textColor = [UIColor blackColor];
    }
    return _topTitleLab;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kColor(246, 246, 246, 1);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAEditShopCell class] forCellReuseIdentifier:@"SAEditShopCell"];
    }
    return _tableView;
}

-(UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth, 105)];
        _headerView.backgroundColor = kColor(246, 246, 246, 1);
        UIView *whiteView = [[UIView alloc] initWithFrame:SARectMake(0, 8, kBaseScreenWidth, 80)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:whiteView];
        UILabel *numberLab = [[UILabel alloc] initWithFrame:SARectMake(99, 48, 50, 40)];
        numberLab.textAlignment = NSTextAlignmentLeft;
        numberLab.text = @"款号";
        numberLab.font = kFont(14);
        numberLab.textColor = [UIColor blackColor];
        [_headerView addSubview:numberLab];
        UILabel *nameLab = [[UILabel alloc] initWithFrame:SARectMake(99, 8, 50, 40)];
        nameLab.textAlignment = NSTextAlignmentLeft;
        nameLab.text = @"名称*";
        nameLab.font = kFont(14);
        nameLab.textColor = [UIColor blackColor];
        [_headerView addSubview:nameLab];
        [nameLab setAttributedStringWithSubString:@"*" color:kRedColor];
        UIView *line = [[UIView alloc] initWithFrame:SARectMake(99, 47, 276, 1)];
        line.backgroundColor = kLineColor;
        [_headerView addSubview:line];
    }
    return _headerView;
}

-(UIImageView *)headerImageV {
    if (!_headerImageV) {
        _headerImageV = [[UIImageView alloc] initWithFrame:SARectMake(25, 12, 64, 72)];
        _headerImageV.image = [UIImage imageNamed:@"Shop_Camera"];
        _headerImageV.contentMode = UIViewContentModeCenter;
        _headerImageV.backgroundColor = kColor(246, 246, 246, 1);
        _headerImageV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ChooseImage)];
        [_headerImageV addGestureRecognizer:tap];
    }
    return _headerImageV;
}

-(UITextField *)productCodeTf {
    if (!_productCodeTf) {
        _productCodeTf = [[UITextField alloc] initWithFrame:SARectMake(150, 48, 200, 40)];
        _productCodeTf.font = kFont(14);
        _productCodeTf.textAlignment = NSTextAlignmentRight;
        _productCodeTf.textColor = kGrayColor;
        _productCodeTf.placeholder = @"不输入款号时，系统自动生成";
        _productCodeTf.delegate = self;
        _productCodeTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_productCodeTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _productCodeTf;
}

-(UIButton *)productCodeBtn {
    if (!_productCodeBtn) {
        _productCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _productCodeBtn.frame = SARectMake(320, 48, 36, 40);
        [_productCodeBtn setImage:[UIImage imageNamed:@"gray_san"] forState:UIControlStateNormal];
        [_productCodeBtn addTarget:self action:@selector(clickedProductCodeBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _productCodeBtn;
}

-(UITextField *)productNameTf {
    if (!_productNameTf) {
        _productNameTf = [[UITextField alloc] initWithFrame:SARectMake(150, 8, 200, 40)];
        _productNameTf.font = kFont(14);
        _productNameTf.textAlignment = NSTextAlignmentRight;
        _productNameTf.textColor = kGrayColor;
        _productNameTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_productNameTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _productNameTf;
}

-(NSMutableArray *)titleMarr {
    if (!_titleMarr) {
        _titleMarr = [NSMutableArray array];
        NSArray *arr1 = @[@"颜色", @"尺码"];
        NSArray *arr2 = @[@"入库价", @"实体零售价", @"微店价", @"折扣"];
        NSArray *arr3 = @[@"商品条码", @"单品条码"];
        //        NSArray *arr4 = @[@"类别", @"供货商", @"创建店铺", @"上架"];
        NSArray *arr4 = @[@"场地", @"经销商", @"季节", @"品牌", @"性别", @"单位",@"类别", @"供货商", @"成分含量", @"执行标准", @"安全类别", @"质量等级",  @"洗涤方式", @"创建店铺", @"上架", @"发布微商城"];
        [_titleMarr addObject:arr1];
        [_titleMarr addObject:arr2];
        [_titleMarr addObject:arr3];
        [_titleMarr addObject:arr4];
    }
    return _titleMarr;
}

-(NSMutableArray *)rightMarr {
    if (!_rightMarr) {
        _rightMarr = [NSMutableArray array];
        NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"", @"", nil];
        NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"", @"", @"", @"", nil];
        NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"", @"", nil];
        //        NSMutableArray *arr4 = [NSMutableArray arrayWithObjects:@"", @"", @"", @"0", nil];
        NSMutableArray *arr4 = [NSMutableArray arrayWithObjects:@"", @"", @"", @"", @"", @"",@"", @"", @"", @"", @"", @"",@"", @"",@"0", @"0", nil];
        [_rightMarr addObject:arr1];
        [_rightMarr addObject:arr2];
        [_rightMarr addObject:arr3];
        [_rightMarr addObject:arr4];
    }
    return _rightMarr;
}

-(NSMutableArray *)typeMarr {
    if (!_typeMarr) {
        _typeMarr = [NSMutableArray array];
        NSArray *arr1 = @[@"1", @"1"];
        NSArray *arr2 = @[@"0", @"0", @"0", @"0"];
        NSArray *arr3 = @[@"0", @"0"];
        NSArray *arr4 = @[@"0", @"0", @"1", @"1", @"1", @"1",@"1", @"1", @"1", @"1", @"1", @"1", @"1", @"0", @"2", @"2"];
        [_typeMarr addObject:arr1];
        [_typeMarr addObject:arr2];
        [_typeMarr addObject:arr3];
        [_typeMarr addObject:arr4];
    }
    return _typeMarr;
}

-(SAProductDetailAPI *)productDetailAPI {
    if (!_productDetailAPI) {
        _productDetailAPI = [SAProductDetailAPI apiWithDelegate:self];
    }
    return _productDetailAPI;
}

-(NSMutableArray *)colorChooseArray {
    if(!_colorChooseArray) {
        _colorChooseArray = [NSMutableArray array];
    }
    return _colorChooseArray;
}

-(NSMutableArray *)sizeChooseArray {
    if (!_sizeChooseArray) {
        _sizeChooseArray = [NSMutableArray array];
    }
    return _sizeChooseArray;
}

-(NSMutableArray *)colorChooseNameArray {
    if(!_colorChooseNameArray) {
        _colorChooseNameArray = [NSMutableArray array];
    }
    return _colorChooseNameArray;
}

-(NSMutableArray *)sizeChooseNameArray {
    if (!_sizeChooseNameArray) {
        _sizeChooseNameArray = [NSMutableArray array];
    }
    return _sizeChooseNameArray;
}

-(UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
        _confirmBtn.frame = SARectMake(254, 0, 53, 44);
        [_confirmBtn addTarget:self action:@selector(clickedConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}

-(NSMutableArray *)categoryChooseArray
{
    if(_categoryChooseArray== nil)
    {
        _categoryChooseArray = [NSMutableArray array];
    }
    return _categoryChooseArray;
}

-(NSMutableArray *)imageChooseArray
{
    if(_imageChooseArray == nil) {
        _imageChooseArray = [NSMutableArray array];
    }
    return _imageChooseArray;
}

-(NSMutableArray *)imageServerChooseArray
{
    if(_imageServerChooseArray == nil) {
        _imageServerChooseArray = [NSMutableArray array];
    }
    return _imageServerChooseArray;
}

-(SAProductUpdateAPI *)productUpdateAPI {
    if (!_productUpdateAPI) {
        _productUpdateAPI = [SAProductUpdateAPI apiWithDelegate:self];
    }
    return _productUpdateAPI;
}

-(SAproductUploadAPI *)productUploadAPI {
    if (!_productUploadAPI) {
        _productUploadAPI = [SAproductUploadAPI apiWithDelegate:self];
    }
    return _productUploadAPI;
}


@end


